package com.fandou.airobot.ui.recording;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.SPUtils;
import com.fandou.airobot.BR;
import com.fandou.airobot.R;
import com.fandou.base.ui.DataBindingConfig;
import com.fandou.base.ui.event.PageClickProxy;
import com.fandou.base.ui.fragment.BaseFragment;
import com.fandou.base.view.NumberStepController;
import com.fandou.base.view.TitleBar;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

/**
 * Create by yangzj at 2020/6/19
 */
public class RecordingFragment extends BaseFragment {

    /**
     * 自动拍照间隔的分钟 sp key
     */
    public final static String SPKEY_RECORDING_INTERVAL = "recordingInterval";

    private RecordingPeriodViewModel recordingPeriodViewModel;

    private NumberStepController numberStep;


    @Override
    protected void initViewModel() {
        recordingPeriodViewModel = getFragmentViewModel(RecordingPeriodViewModel.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.ai_robot_recording_layout, recordingPeriodViewModel)
                .addBindingParam(BR.title, R.string.recording_title)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recordingPeriodViewModel.getRecordingPeriodLiveData().observe(getViewLifecycleOwner(), time -> {
            recordingPeriodViewModel.time.setValue(time);
        });
        recordingPeriodViewModel.requestRecordingPeriod();


        numberStep = view.findViewById(R.id.monitor_setting_period_controller);
        view.findViewById(R.id.ai_robot_recording_start_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int newInterval = numberStep.getNumber();
                if (newInterval <= 0) {
                    showWarnToast("拍照间隔不能为0");
                    return;
                }
                toCamera(numberStep.getNumber());

            }
        });

        //查看历史记录
        TitleBar titleBar = view.findViewById(R.id.titleBar);
        titleBar.setOnRightClickListener(new TitleBar.OnTvRightClickListener() {
            @Override
            public void onTitleBarTvRightClick() {
//                MyFragmentUtilsWrapper.addFromBottom(((AppCompatActivity) mContext).getSupportFragmentManager(), RecordHistoryFragment.newInstance(),android.R.id.content);
                RecordHistoryActivity.start(mContext);
            }
        });

        observeModifyIntervalFromVoice();
    }

    /**
     * 订阅从语音控制里修改间隔
     */
    private void observeModifyIntervalFromVoice() {
        LiveEventBus.get(AsrExitRecordingEvent.class.getName(), AsrExitRecordingEvent.class).observe(this, new Observer<AsrExitRecordingEvent>() {
            @Override
            public void onChanged(AsrExitRecordingEvent event) {
                if (event.getAction() == AsrExitRecordingEvent.MODIFY_INTERVAL) {
                    SPUtils.getInstance().put(RecordingFragment.SPKEY_RECORDING_INTERVAL, event.getInterval());
                    if (numberStep != null) {
                        numberStep.setNumber(event.getInterval());
                    }
                }

            }
        });
    }

    private void toCamera(int newInterval) {
        AndPermission.with(this)
                .runtime()
                .permission(Permission.Group.STORAGE, Permission.Group.CAMERA)
                .onGranted(permissions -> {
                    SPUtils.getInstance().put(SPKEY_RECORDING_INTERVAL, newInterval);
                    RecordCameraActivity.start(mContext, newInterval);
                })
                .onDenied(permissions -> {
                    showErrorToast("请先授予摄像机权限");
                })
                .start();
    }

    @Override
    public void onResume() {
        super.onResume();
        int lastInterval = SPUtils.getInstance().getInt(SPKEY_RECORDING_INTERVAL, numberStep.getMinNumber());
        numberStep.setNumber(lastInterval);
    }

    public class ClickProxy extends NumberStepController.NumberStepClickProxy implements PageClickProxy {

        @Override
        public void back() {
            nav().navigateUp();
        }

        @Override
        public void add() {
            super.add();
        }

        @Override
        public void dec() {
            super.dec();
        }
    }

    public static RecordingFragment newInstance() {

        Bundle args = new Bundle();

        RecordingFragment fragment = new RecordingFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
